var app = getApp()  
Page({  
  data: {  
    navbar: ['闲置', '图书'], 
    navType: ['全部','待收货','借阅中','已逾期','已归还'],
    navType1: ['全部','待付款','待发货',"待收货","已完成"],     
    currentTab: 0,
    currentTab1:0,
    index:0,
    index1:0,
    pagenum:1,
    goodDatas:[]
  },  
  navbarTap: function(e){  
    var that = this;
    this.setData({  
      currentTab: e.currentTarget.dataset.idx,
      goodDatas:[],
      currentTab1:0,
      pagenum:1
    }) 
    that.getDataList();
  },
  navbarTap1: function(e){  
    var that = this;
    this.setData({  
      currentTab1: e.currentTarget.dataset.idx,
      goodDatas:[],
      pagenum:1
    })  
    that.getDataList();
  },
  onShow:function(e){
    var that = this;
    that.setData({
        pagenum:1,
        goodDatas:[]
    })
      that.getDataList();
  },
  onLoad:function(e){
    var that = this;
    this.setData({
      currentTab:1
    })
    if(undefined != e.from){
        this.setData({
            currentTab:e.from
        })
    }

  },
  del:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title:'提示',
      content:'确定要删除吗？',
      showCancel:true,
      success:function(res){
        if (res.confirm) {
          // console.log('用户点击确定')
          wx.request({
            url:getApp().data.server + '/index.php/Tongcheng/Book/delOrderById?id='+id,
            success:function(e){
                console.log('fanhui'+e.data)
              if(1 == e.data){
                wx.showToast({
                  title:'删除成功',
                  icon: 'success',
                  duration: 2000
                })
                  setTimeout(function(){
                      that.setData({
                          pagenum:1,
                          goodDatas:[]
                      })
                      that.getDataList();
                  },2000)

              }else{
                wx.showToast({
                  title:'删除失败',
                  icon: 'success',
                  duration: 2000
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
  ,guiHuan:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title:'提示',
      content:'确定要归还吗？',
      showCancel:true,
      success:function(res){
        if (res.confirm) {
          // console.log('用户点击确定')
            wx.request({
                url: app.data.server+'/index.php/Tongcheng/Book/guiHuanBook?id='+id,
                success:function(d){
                    // console.log('123'+JSON.stringify(d.data));
                    if(1 == d.data){
                        wx.showToast({
                            title:'确认成功',
                            icon: 'success',
                            duration: 2000
                        })
                        setTimeout(function(){
                            that.setData({
                                pagenum:1,
                                goodDatas:[]
                            });
                            that.getDataList();
                        },2000);
                    }else{
                        wx.showToast({
                            title:'确认失败',
                            icon: 'success',
                            duration: 2000
                        })
                    }
                }
            })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  },
  xuJie:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    var oid = e.currentTarget.dataset.oid;
    wx.navigateTo({
      url:'/pages/find/xuejie?id=' + id + '&oid=' + oid
    })
  },
  // bindPickerChange:function(e){
  // 	var that = this;
  // 	that.setData({
  // 		index:e.detail.value,
  // 		index1:e.detail.value
  // 	})
  // } 
  getDataList:function(e){
    var that = this;
    wx.request({
        url: app.data.server+'/index.php/Tongcheng/Book/getOrderListByUid?pagenum='+that.data.pagenum+'&type='+that.data.currentTab+'&status='+that.data.currentTab1+'&uid='+getApp().globalData.userInfo.uid,
        success:function(d){
            // console.log('123'+JSON.stringify(d.data));
            wx.hideLoading();
            if(0 == d.data){

                return;
            }
            that.setData({
              goodDatas:that.data.goodDatas.concat(d.data)
            })
        }
    })
  },
    //完成订单
    yiwancheng:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        wx.showModal({
            title:'提示',
            content:'确定已完成该订单吗？',
            showCancel:true,
            success:function(res){
                if (res.confirm) {
                    wx.request({
                        url: app.data.server+'/index.php/Tongcheng/Book/wanChengOrderById?id='+id,
                        success:function(d){
                            // console.log('123'+JSON.stringify(d.data));
                            if(1 == d.data){
                                wx.showToast({
                                    title:'确认成功',
                                    icon: 'success',
                                    duration: 2000
                                })
                                that.setData({
                                    goodDatas:[],
                                    pagenum:1
                                })
                                that.getDataList();
                            }else{
                                wx.showToast({
                                    title:'确认失败',
                                    icon: 'success',
                                    duration: 2000
                                })
                            }
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    qurenshouhuo:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        wx.showModal({
            title:'提示',
            content:'确定收货吗？',
            showCancel:true,
            success:function(res){
                if (res.confirm) {
                    wx.request({
                        url: app.data.server+'/index.php/Tongcheng/Book/quRenShouHuo?id='+id,
                        success:function(d){
                            // console.log('123'+JSON.stringify(d.data));
                            if(1 == d.data){
                                wx.showToast({
                                    title:'确认成功',
                                    icon: 'success',
                                    duration: 2000
                                })
                                setTimeout(function(){
                                    that.setData({
                                        goodDatas:[],
                                        pagenum:1
                                    })
                                    that.getDataList();
                                },2000);
                            }else{
                                wx.showToast({
                                    title:'确认失败',
                                    icon: 'success',
                                    duration: 2000
                                })
                            }
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    //去支付
    zhifu:function(e){
        var price = e.currentTarget.dataset.price,
            freight=e.currentTarget.dataset.freight,
            id=e.currentTarget.dataset.id,
            oid=e.currentTarget.dataset.oid;
        wx.showModal({
            title:'提示',
            content:'确定付款吗？',
            showCancel:true,
            success:function(res){
                if (res.confirm) {
                    wx.navigateTo({
                        url: '/pages/index/order/index?id='+id+'&oid='+oid+'&price='+price+'&freight='+freight+'&uid=zhifu'
                    });
                }
            }
        })
    },
    toInfo:function(e){
        var that = this,id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/pages/order/orderInfo?id='+id
        })
    },
    //指定到借图详情页面
    toBookInfo:function(e){
        var that = this;
        var id = e.currentTarget.dataset.otherid;
        var oid = e.currentTarget.dataset.oid;
        var ostatus = e.currentTarget.dataset.status;
        var path = "";
        if(1 == ostatus || 2 == ostatus ){
            path = '/pages/find/sureJie?id=' + id + '&oid=' + oid + '&ostatus='+ostatus
        }else if(3 == ostatus){
            path = '/pages/find/xuejie?id=' + id + '&oid=' + oid + '&ostatus='+ostatus
        }
        wx.navigateTo({
            url:path
        })
    },
    onReachBottom: function (e) {
        // var that = this;
        // // 显示加载图标
        // wx.showLoading({
        //     title: '玩命加载中',
        // })
        // that.setData({
        //     pageNum:1,
        //     xieWenData:[]
        // })
        // this.getDataList();
    },
    onPullDownRefresh:function(e){
        // console.log('下拉刷新')
        wx.showLoading({
            title: '玩命加载中',
        })
        var that = this;
        that.setData({
            pagenum:that.data.pagenum+1

        })
        this.getDataList();
    },
    //续借订单
    xujiedingdan:function(e){
      var that = this;
      var oid = e.currentTarget.dataset.id;
      console.log('xujieoid'+oid)
     wx.navigateTo({
       url: '/pages/order/xujieorder?pid='+oid
     }) 
    }
})  